package org.dromara.commission.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.commission.domain.LzshAgentInfoRelation;
import org.dromara.commission.domain.bo.LzshAgentInfoRelationBo;
import org.dromara.commission.domain.vo.LzshAgentInfoRelationImportVo;
import org.dromara.commission.service.ILzshAgentInfoRelationService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : sxl
 * @date : 2024/7/27 11:36
 * @Version: 1.0
 */
@Slf4j
public class LzshAgentInfoRelationImportListener extends AnalysisEventListener<LzshAgentInfoRelationImportVo> implements ExcelListener<LzshAgentInfoRelationImportVo> {
    private final ILzshAgentInfoRelationService lzshAgentInfoRelationService;

    private final Boolean isUpdateSupport;

    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();
    private List<LzshAgentInfoRelation> list = new ArrayList<>();


    public LzshAgentInfoRelationImportListener(Boolean isUpdateSupport) {
        this.lzshAgentInfoRelationService = SpringUtils.getBean(ILzshAgentInfoRelationService.class);;
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }

    @Override
    public ExcelResult<LzshAgentInfoRelationImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
                }
                return successMsg.toString();
            }

            @Override
            public List<LzshAgentInfoRelationImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(LzshAgentInfoRelationImportVo relationImportVo, AnalysisContext analysisContext) {
        LzshAgentInfoRelation lzshAgentInfoRelation = BeanUtil.toBean(relationImportVo, LzshAgentInfoRelation.class);
        lzshAgentInfoRelation.setTenantId("000000");
        lzshAgentInfoRelation.setUpdateBy(operUserId);
        list.add(lzshAgentInfoRelation);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (lzshAgentInfoRelationService.insertBatch(list,list.size())) successNum = list.size();
    }
}
